package com.xb.loan.encryptor.comm;


import com.xb.loan.encryptor.bean.LotteryBean;
import com.xb.loan.encryptor.util.LogFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BasicSocketManager {

	private static Log log = LogFactory.getLog(BasicSocketManager.class);
	
	private static BasicSocketFactory basicSocketFactory = new BasicSocketFactory();
	
	// 获取一个可用连接发送信息
	@SuppressWarnings("unchecked")
	public static <T> T send(LotteryBean obj) {
		
		Object response = null;
		
		BasicSocketClient client = null;
		
		try {
			
			client = basicSocketFactory.getInstance();
			
			client.updateStatusForce(BasicSocketClient.STATUS_BUSY);
			
			client.send(obj);
			
			response = client.receive();
		
		} catch (Exception e) {
			log.error(LogFormat.formatMsg("TerminalSocketManager.send", client.getRemoteSocketAddress().toString(), "send have an exception.."),e);
		} finally{
			
			if(client != null){
				
				client.close();
			}
		}
		
		return (T) response;
	}
	
}
